import { sys } from "cc";

export const enum LOG_LEVEL_TYPES { DEBUG, LOG, INFO, WARN, ERROR }
const Log_Level_Names: Array<string> = ["debug", "log", "info", "warn", "error"];

// http://81.70.217.34:9400/set_st?status=1

export class Logger {
    public static tag: string = "[HaoJslog]"; //可以设置当前游戏的前缀

    public static LEVEL: number = LOG_LEVEL_TYPES.LOG; //当前Logger等级

    public static Log_Color_Config: Array<string> = [
        "color:#890;font-size:10px;",
        "color:#000;font-size:11px;",
        "color:#09f;font-size:12px;",
        "color:#f90;font-size:13px;",
        "color:#f00;font-size:15px;",
    ];

    private static Terminal_Log: boolean = false;

    public static formatNow() {
        let date: Date = new Date(); //后端返回的时间戳是秒
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds();
    }

    private static getLogPreKey(nowLevel: number): string {
        let str: string = "[" + Logger.formatNow() + "] " + Logger.tag + " ["
            + Log_Level_Names[nowLevel] + "] ";
        return str;
    }

    /**
   * 原生平台上不能直接打印object和array，因此这里将object和array转换为字符串进行输出，方便在 Android Logcat 中直接看 log 结果
   */
    private static formatNative(...args: any[]): string {
        let msg: string = "";
        args.forEach((value: any, index: number, array: any[]) => {
            if (value == null) {
                msg += "null";
            } else {
                const valType = typeof value;
                if (valType === "string" || valType === "number") {
                    msg += value;
                } else {
                    if(typeof(value) == "object"){
                        try {
                            msg += JSON.stringify(value);
                        } catch (error) {
                            msg += value.toString()
                        }
                    }
                }
            }
            if (index + 1 < array.length) {
                msg += ",";
            }
        });
        return msg;
    }

    public static debug(...params: any) {
        if (Logger.LEVEL > LOG_LEVEL_TYPES.DEBUG) {
            return;
        }
        let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.DEBUG);
        // let fileStr: string = str + params.join(" ");
        // LogErrorFileUtil.debug(fileStr);
        if(sys.isNative){
            console.log(str, this.formatNative(...params))
        }else{
            console.log(str, ...params)
        }
    }

    public static log(...params: any) {
        if (Logger.LEVEL > LOG_LEVEL_TYPES.LOG) {
            return;
        }
        let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.LOG);
        // LogErrorFileUtil.log(fileStr);
        if(sys.isNative){
            console.log(str, this.formatNative(...params))
        }else{
            console.log(str, ...params)
        }

      
    }

    public static info(...params: any) {
        if (Logger.LEVEL > LOG_LEVEL_TYPES.INFO) {
            return;
        }
        let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.INFO);
        if(sys.isNative){
            console.info(str, this.formatNative(...params))
        }else{
            console.info(str, ...params)
        }
     
    }

    public static warn(...params: any) {
        if (Logger.LEVEL > LOG_LEVEL_TYPES.WARN) {
            return;
        }
        let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.WARN);
        if(sys.isNative){
            console.warn(str, this.formatNative(...params))
        }else{
            console.warn(str, ...params)
        }
    }

    public static error(...params: any) {
        if (Logger.LEVEL > LOG_LEVEL_TYPES.ERROR) {
            return;
        }
        let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.ERROR);
        if(sys.isNative){
            console.error(str, this.formatNative(...params))
        }else{
            console.error(str, ...params)
        }
      
    }
}
